<?
	//include('conn.php');

	$host='localhost';
	$user='invoiceappprod';
	$pwd='Jalg0anid@';
	$db='invoiceappprod';

	/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
	
	$link = mysql_connect($host,$user,$pass);
	mysql_select_db($name,$link);
	
	//get all of the tables
	if($tables == '*')
	{
		$tables = array();
		$result = mysql_query('SHOW TABLES');
		while($row = mysql_fetch_row($result))
		{
			$tables[] = $row[0];
		}
	}
	else
	{
		$tables = is_array($tables) ? $tables : explode(',',$tables);
	}
	
	//cycle through
	foreach($tables as $table)
	{
		$result = mysql_query('SELECT * FROM '.$table);
		$num_fields = mysql_num_fields($result);
		
		//$return.= 'DROP TABLE '.$table.';';
		$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
		$return.= "\n\n".$row2[1].";\n\n";
		
		for ($i = 0; $i < $num_fields; $i++) 
		{
			while($row = mysql_fetch_row($result))
			{
				$return.= 'INSERT INTO '.$table.' VALUES(';
				for($j=0; $j<$num_fields; $j++) 
				{
					$row[$j] = addslashes($row[$j]);
					$row[$j] = ereg_replace("\n","\\n",$row[$j]);
					if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
					if ($j<($num_fields-1)) { $return.= ','; }
				}
				$return.= ");\n";
			}
		}
		$return.="\n\n\n";
	}
	
	//save file
	$backupfile = 'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql';
	$handle = fopen($backupfile,'w+');
	fwrite($handle,$return);
	fclose($handle);
	return $backupfile;
}

	//$backupfile = backup_tables('50.63.227.21','invoiceappuat','Jalg0anit@','invoiceappuat');
	$backupfile = backup_tables($host,$user,$pwd,$db);
?>	

<?
//define the receiver of the email
$to = 'gyansada@gmail.com,rajeshcsingh@gmail.com';
//define the subject of the email
$subject = 'SQL Backup: Shree Communication for '.$db;
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
//$headers = "MIME-Version: 1.0\r\n";
$headers = "From: mail@ragyventures.com
Reply-To: mail@ragyventures.com";
$headers .= "MIME-Version: 1.0:";
//add boundary string and mime type specification
$headers .= "
Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks

$attachment = chunk_split(base64_encode(file_get_contents($backupfile)));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
Content-Type: application/sql; name="<?echo $backupfile?>" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?> 